//
// Created by Administrator on 2023/10/8.
//
#include <vector>
#include <iostream>
#include <unordered_set>

using namespace std;

class Solution {
public:
    int minOperations(std::vector<int> &nums, int k) {
        int n = nums.size();
        long long u = (2LL << k) - 2; // 1~k
        long long s = 0;
        for (int i = n - 1;; --i) {
            s |= 1LL << nums[i];
            if ((s & u) == u) {
                return n - i;
            }
        }
    }

    int minOperations2(std::vector<int> &nums, int k) {
        int n = nums.size();
        unordered_set<int> s;
        for (int i = 1; i <= k; ++i) {
            s.insert(i);
        }
        for (int i = n - 1; i >= 0; i--) {
            s.erase(nums[i]);
            if (s.empty()) {
                return n - i;
            }
        }
        return n;
    }
};


int main() {
    Solution s;
    vector<int> v{3, 1, 5, 4, 2};
    cout << s.minOperations2(v, 3) << endl;
    return 0;
}